package com.cn.xiaonuo.common.txim;

import com.alibaba.fastjson.JSONObject;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

//

/**
 *
 *  tls-sig-api-v2-1.1.jar libs里面的包 用来腾讯im签名的
 *
 */
@RestController
@CrossOrigin
public class TxImController {

    private static Logger logger = LoggerFactory.getLogger(TxImController.class);

    private static  final  String txShowOnlinePeople="txShowOnlinePeople:";


    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @ApiOperation(value = "腾讯Im聊天签名" ,httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expire", value = "时间 秒 例如 604800 = 7 x 24 x 60 x 60 = 604800 = 7 天", dataType = "integer"),
            @ApiImplicitParam(name = "userId", value = "userId  类型的签名", dataType = "integer"),
    })
    @RequestMapping(value = "/api/common/txImSign", method = RequestMethod.POST)
    public @ResponseBody
    BusinessMessage txImSign(@RequestParam(value = "userId", required = false) Long userId,
                             @RequestParam(value = "expire", required = false) Long expire
                             ){
        BusinessMessage message = new BusinessMessage();
        if (null==userId){
            message.setSuccess(false);
            message.setMessage("用户号不能为空");
            return message;
        }
        if(null==expire){
            expire=60480000L;
        }
        String sign= TLSSigAPIv2.genUserSig( String.valueOf(userId),expire);
        message.setData(sign);
        message.setSuccess(true);
        return message;
    }


    public static String sendHttpPostJsonLimte(String url, String body) {
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json");
            httpPost.setEntity(new StringEntity(body, "utf-8"));
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectionRequestTimeout(2000)
                    .setConnectTimeout(1000)
                    .setSocketTimeout(1000).build();
            httpPost.setConfig(requestConfig);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            String responseContent = EntityUtils.toString(entity, "UTF-8");
            logger.info(responseContent);
            response.close();
            httpClient.close();
            return responseContent;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }



}
